﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Pub;
using JCNT.DAL;
using System.Threading;

namespace JCNT.NoteApp
{
    public partial class FImport : Form
    {
        CTreeII ctree = new CTreeII();
        public Boolean b_Run = false;
        public Boolean b_OK = false;
        public string sInfo = "";

        public FImport()
        {
            InitializeComponent();
        }

        private void btnOpenDir_Click(object sender, EventArgs e)
        {
            if (tboxPath.Text != string.Empty) folderBrowserDialog1.SelectedPath = tboxPath.Text;
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                tboxPath.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void FImport_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            CDebug.EnterMethod("查找文件");

            try
            {
                string sExp = cboxExp.Text;
                string sPath = tboxPath.Text;

                if (sPath != string.Empty)
                {
                    List<string> fileLst = new List<string>();

                    string sExp_DB = "";
                    string sExp_Tem = "";
                    string strSQL0 = "SELECT [val] FROM wp_SysOpt WHERE Opt='F_Import_Exp'";
                    DataSet ds1 = Program.ado1.sel1(strSQL0);
                    if (!CDB.dsIsNull(ds1)) sExp_DB = ds1.Tables[0].Rows[0][0].ToString();
                    sExp_Tem = sExp_DB;
                    //最后一个扩展名补一个空格，把每一个扩展名补全为*.xxx + 空格的形式
                    sExp_DB += " ";

                    string[] sExpTemps = sExp.Split(',');
                    for (int i = 0; i < sExpTemps.Length; i++)
                    {
                        if (sExp_DB.IndexOf(sExpTemps[i].Trim() + " ") == -1)
                            sExp_Tem += " " + sExpTemps[i];
                    }
                    string strSQL1 = "UPDATE wp_SysOpt SET [val]='" + sExp_Tem + "' WHERE Opt='F_Import_Exp'";
                    Program.ado1.cmd1(strSQL1);

                    getFileList(fileLst, sPath, sExp, chkSchChild.Checked);

                    chkLBox.Items.Clear();
                    for (int i = 0; i < fileLst.Count; i++)
                    {
                        chkLBox.Items.Add(fileLst[i]);
                    }

                    sInfo = "" + chkLBox.Items.Count;

                    btnSelAll_Click(btnSelAll, null);
                }
                else
                {
                    MessageBox.Show("路径不能为空!");
                }

            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
            }

            CDebug.ExitMethod("查找文件");
        }

        public void getFileList(List<String> fileLst, string sPath, string sExp, Boolean b_SchChild)
        {
            if (fileLst == null) fileLst = new List<string>();

            DirectoryInfo dirInfo = new DirectoryInfo(sPath);
            string[] sExps = sExp.Split(',');
            for (int i = 0; i < sExps.Length; i++)
            {
                foreach (FileInfo fInfo in dirInfo.GetFiles(sExps[i].Trim()))
                {
                    //CDebug.Send(fInfo.FullName);
                    fileLst.Add(fInfo.FullName);
                }
            }

            //遍历子目录
            if (b_SchChild)
            {
                foreach (DirectoryInfo dInfo in dirInfo.GetDirectories())
                {
                    string sPath_Child = dInfo.FullName;
                    getFileList(fileLst, sPath_Child, sExp, b_SchChild);
                }
            }
        }

        private void btnSelAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < chkLBox.Items.Count; i++)
            {
                chkLBox.SetItemChecked(i, true);
            }
        }

        private void btnUnSelAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < chkLBox.Items.Count; i++)
            {
                chkLBox.SetItemChecked(i, false);
            }
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            if (b_Run == false)
            {
                btn_Enable(false);

                Thread thread1 = new Thread(new ThreadStart(importFile));
                thread1.Start();
            }
            else
            {
                MessageBox.Show("文件正在导入，请稍等！");
            }

        }

        public void btn_Enable(Boolean b1)
        {
            groupBox1.Enabled = b1;
            groupBox2.Enabled = b1;
            btnSearch.Enabled = b1;
            btnImport.Enabled = b1;
            btn_Cancel.Enabled = b1;
        }


        private void importFile()
        {
            CDebug.EnterMethod("开始导入文件");

            try
            {
                b_Run = true;
                //开始导入文件
                for (int i = 0; i < chkLBox.Items.Count; i++)
                {
                    if (chkLBox.GetItemChecked(i))
                    {
                        string sFileName = chkLBox.Items[i].ToString();
                        //\\替换为\,并分割为数组
                        string sPath_Tem = tboxPath.Text;
                        sPath_Tem = sPath_Tem.Replace(@"\\", @"\");
                        if (sPath_Tem[sPath_Tem.Length - 1] == '\\') sPath_Tem.Remove(sPath_Tem.Length - 1);
                        string[] s1 = sPath_Tem.Split('\\');
                        int i1 = tboxPath.Text.Length;
                        int i2 = sFileName.Length;
                        string sPath = sFileName.Substring(i1, i2 - i1);
                        if (chkCrtRoot.Checked)
                        {
                            sPath = s1[s1.Length - 1] + @"\" + sPath;
                            sPath = sPath.Replace(@"\\", @"\");

                        }

                        string sRootID = Program.ftree.ID;
                        ctree.ImportFile(sRootID, sPath, sFileName);
                        sInfo = "" + (i + 1) + "/" + chkLBox.CheckedItems.Count;

                        Thread.Sleep(1000);
                    }

                }
                sInfo = "";
                b_OK = true;
                b_Run = false;
                MessageBox.Show("文件导入完成！");

            }
            catch (System.Exception ex)
            {
                MessageBox.Show("导入文件出现错误！\n" + ex.ToString());
                CDebug.SendError(ex.ToString());
            }

            CDebug.ExitMethod("开始导入文件");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Text = "文件导入 " + sInfo;
            if (b_OK)
            {
                btn_Cancel_Click(btn_Cancel, null);
                btn_Enable(true);
                timer1.Enabled = false;
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            chkLBox.Items.Clear();
            this.Close();
        }

        private void FImport_Load(object sender, EventArgs e)
        {
            cboxExp.Items.Clear();
            string strSQL = "SELECT [val] FROM wp_SysOpt WHERE Opt='F_Import_Exp'";
            DataSet ds1 = Program.ado1.sel1(strSQL);
            if (!CDB.dsIsNull(ds1))
            {
                string sExp = ds1.Tables[0].Rows[0][0].ToString();
                //多个空格替换为一个空格
                for (int i = 0; i < 5; i++) sExp = sExp.Replace("  ", " ");
                string[] sExps = sExp.Split(' ');
                for (int i = 0; i < sExps.Length; i++)
                {
                    cboxExp.Items.Add(sExps[i].Trim());
                }
            }
        }

    }
}
